package web_controller

import (
	"github.com/duke-git/lancet/v2/convertor"
	"github.com/gin-gonic/gin"
	"strconv"
	"transport/framework/fw_db/fw_db_self"
	"transport/framework/fw_event"
	"transport/framework/fw_log"
	"transport/framework/fw_middleware"
	"transport/framework/fw_utils"
	"transport/web/web_mapper"
	"transport/web/web_model"
)

type merchantsController struct {
}

func (controller *merchantsController) ItemById(c *gin.Context) {
	id, _ := convertor.ToInt(c.Param("id"))
	mapper := web_mapper.MerchantsMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	item, err := mapper.QueryById(id)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, item, "ok")
}
func (controller *merchantsController) ItemByCode(c *gin.Context) {
	mapper := web_mapper.MerchantsMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	item, err := mapper.QueryByCode(c.Param("code"))
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, item, "ok")
}
func (controller *merchantsController) DelById(c *gin.Context) {
	id, _ := convertor.ToInt(c.Param("id"))
	mapper := web_mapper.MerchantsMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	err := mapper.DeleteById(id)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, "", "ok")
}
func (controller *merchantsController) DelByIds(c *gin.Context) {
	var ids []int64
	err := c.ShouldBindJSON(&ids)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	mapper := web_mapper.MerchantsMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	err = mapper.DeleteByIds(ids)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, "", "ok")
}
func (controller *merchantsController) CreateOrUpdateItem(c *gin.Context) {
	var data web_model.TransMerchants
	err := c.ShouldBindJSON(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	mapper := web_mapper.MerchantsMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	mapperOrder := web_mapper.OrderrMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	_, err = mapper.CreateOrUpdate(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	err = mapperOrder.UpdateRouteByCustomerCode(data.RouteName, data.Code)
	if err != nil {
		fw_log.Slogger.Info(err.Error())
	}
	fw_utils.OK(c, "", "ok")
}
func (controller *merchantsController) CreateItem(c *gin.Context) {
	var data web_model.TransMerchants
	err := c.ShouldBindJSON(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	mapper := web_mapper.MerchantsMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	ret, err := mapper.Create(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, ret, "ok")
}

func (controller *merchantsController) SyncInfo(c *gin.Context) {
	fw_event.EventChanDelivery(fw_event.EVENT_CMD_SYNC_MERCHANTS_INFO, "", "", "")
	fw_utils.OK(c, "", "ok")
}

func (controller *merchantsController) UpdateItem(c *gin.Context) {
	var data web_model.TransMerchants
	err := c.ShouldBindJSON(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	mapper := web_mapper.MerchantsMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	ret, err := mapper.Update(&data)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.OK(c, ret, "ok")
}
func (controller *merchantsController) Page(c *gin.Context) {
	var err error
	var from, limit = 1, 10
	if tmp := c.Request.FormValue("pageSize"); tmp != "" {
		limit, _ = strconv.Atoi(tmp)
	}
	if tmp := c.Request.FormValue("pageIndex"); tmp != "" {
		from, _ = strconv.Atoi(tmp)
	}
	name := c.Request.FormValue("name")
	county := c.Request.FormValue("county")
	routeId := c.Request.FormValue("routeId")
	mapper := web_mapper.MerchantsMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	items, total, err := mapper.Page(from, limit, name, county, routeId)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.PageOK(c, items, total, from, limit, "ok")
}
func (controller *merchantsController) List(c *gin.Context) {
	var err error
	mapper := web_mapper.MerchantsMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	items, total, err := mapper.List()
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.ListOK(c, items, total, "ok")
}

func MerchantsRoute(r *gin.Engine) {
	controller := merchantsController{}
	v1 := r.Group("/trans").Use(fw_middleware.JwtAuthMiddleware())
	{
		v1.GET("/v1/merchants/page", controller.Page)
		v1.GET("/v1/merchants/list", controller.List)
		v1.GET("/v1/merchants/:id", controller.ItemById)
		v1.GET("/v1/merchantsByCode/:code", controller.ItemByCode)
		v1.DELETE("/v1/merchants/:id", controller.DelById)
		v1.DELETE("/v1/merchants/batchByIds", controller.DelByIds)
		v1.PUT("/v1/merchants", controller.UpdateItem)
		v1.POST("/v1/merchants", controller.CreateItem)
		v1.POST("/v1/merchantsSync", controller.SyncInfo)
		v1.POST("/v1/merchantsItemCreateOrUpdate", controller.CreateOrUpdateItem)
	}
}
